package net.example.lesson5.lock;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest {
    public Object syncLock = new String();
    public ReentrantLock reentrantLock = new ReentrantLock();
    private int c;
    public void syncPrint(){
        synchronized (syncLock){
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            syncAdd();
            System.out.println(c);
        }
    }
    public void syncAdd(){
        synchronized (syncLock){
            System.out.println(Thread.currentThread().getName()+" 执行Add");
            c += 10;
        }
    }
    public void reentrantPrint(){
        reentrantLock.lock();
        reentrantAdd();
        System.out.println(c);
        reentrantLock.unlock();
    }
    public void reentrantAdd(){
        reentrantLock.lock();
        c += 10;
        reentrantLock.unlock();
    }

    public static void main(String[] args) {
        ReentrantLockTest test = new ReentrantLockTest();
//        test.syncPrint();
//        test.reentrantPrint();

        new Thread(()->test.syncPrint()).start();
        new Thread(()->test.syncAdd()).start();
    }

}
